/**
 * 简单的注入器
 */
export class Injector {
    
    /**类型字典*/
    private static _injectedMap: Map<string, any> = new Map<string, any>();
    /**实例字典*/
    private static _instanceMap: Map<string, any> = new Map<string, any>();

    /**
     * 注入
     * @param key 
     * @param clazz   类型或实例
     */
    static inject(customKey: string, clazz: any): void {
        if (clazz instanceof Function) {
            this._injectedMap.set(customKey, clazz);
        } else {
            this._instanceMap.set(customKey, clazz);
        }
    }

    /**
     * 获取已注入的类型实例
     */
    static getInject(customKey: string): any | null {
        let instance: any = this._instanceMap.get(customKey);
        if (instance) {
            return instance;
        }
        let clazz: any | undefined = this._injectedMap.get(customKey);
        if (clazz === undefined) {
            throw new Error(customKey + "未注入！");
        }
        instance = new clazz();
        this._instanceMap.set(customKey, instance);
        return instance;
    }
}
